<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class OrdersController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
	public function index(){
		$order_no = I('order_no')?I('order_no'):null;
		$type = I('type')?I('type'):null;
		if(!empty($order_no)){
			$where['order_no']=array('like','%'.$order_no.'%');
		}
		if(!empty($type)){
			$where['order_status']=$type;
		}else{
		    $where['order_status'] = array('neq','-9');
		}
		$count=M('rob_orders')->where($where)->order('add_time desc')->count();
		$Page=new Page($count,8);
		//setPageParameter($Page, array('account'=>$account));
		$show       = $Page->show();
		$list =  M('rob_orders')->where($where)
		->limit($Page->firstRow.','.$Page->listRows)->order('add_time desc')->select();
		foreach ($list as &$v){
			$nickname = M('Member')->where(array('openid'=>$v['open_id']))->find();
			$v['nickname'] = $nickname['nickname'];
			if($v['send_money'] == '9999.00'){
			    $v['send_money'] = "邮费到付";
			}
			if($v['send_money'] == '0.00'){
			    $v['send_money'] = "包邮";
			}
		}
		//dump($list);die;
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	public function detail(){
        $order_id = I('order_id');
        $is_tui  = I('is_tui');
        $list = M('rob_orders o')
                ->field('o.*,o.add_time as oadd_time,m.*')
                ->join('yang_member m ON m.openid=o.open_id')
                ->where('o.order_id ='.$order_id)
                ->find();
        $goods = M('rob_goods_orders og')
                ->field("og.*,g.*")
                ->join('yang_goods g ON og.goods_id=g.goods_id')
                ->where('og.order_id ='.$order_id)
                ->select();
     
        //dump($list);
        //dump($goods);
        $this->assign('list',$list);
        $this->assign('goods',$goods);
        $this->display();
	}
	public function setStatus(){
		$order_id = I('order_id');
		$kuaidi = I('kuaidi');
		$is_tui = I('is_tui');
		$kuaidigongsi=I("kuaidigongsi");
		$re = M('rob_orders')->where('order_id ='.$order_id)->find();
		//dump($re);die;
		if($re['order_status'] == 2 && $re['kuaidi'] == $kuaidi){
			$this->error('已经是发货状态了！');
		}else{
			if($re['order_status'] == 1 && $kuaidi == ''){
				$this->error("请选择是否发货并填写快递单号");
			}
			if($re['order_status'] == 1 && $kuaidigongsi == ''){
				$this->error("请选择是否发货并填写快递公司");
		
			}
		}
		$time = time();
		$res  = M('rob_orders')->where('order_id ='.$order_id)->save(array('order_status'=>2,'kuaidigongsi'=>$kuaidigongsi,'kuaidi'=>$kuaidi,'send_time'=>$time));
		if($res){
		    $this->error('操作成功',U('Admin/Orders/index'));
		}else{
		    $this->error('操作失败');
		}
	}
	//退款列表
	public function tuiIndex(){
	    $order_no = I('order_no')?I('order_no'):null;
	   // $type = I('type')?I('type'):null;
	    if(!empty($order_no)){
	        $where['order_no']=array('like','%'.$order_no.'%');
	    }
	        $where['order_status']=4;
	    
	    $count=M('rob_orders')->where($where)->order('add_time desc')->count();
	    $Page=new Page($count,8);
	    //setPageParameter($Page, array('account'=>$account));
	    $show       = $Page->show();
	    $list =  M('rob_orders')->where($where)
	    ->limit($Page->firstRow.','.$Page->listRows)->order('add_time desc')->select();
	    foreach ($list as &$v){
	        $nickname = M('Member')->where(array('openid'=>$v['open_id']))->find();
	        $v['nickname'] = $nickname['nickname'];
	    if($v['send_money'] == '9999.00'){
			    $v['send_money'] = "邮费到付";
			}
			if($v['send_money'] == '0.00'){
			    $v['send_money'] = "包邮";
			}
	    }
	    //dump($list);die;
	    $this->assign('list',$list);
	    $this->assign('page',$show);
	    $this->display();
	}
	//退款操作
	public function setTuiStatus(){
		$where['goods_id'] = I('goods_id');
		$where['attr_name'] = I('attr_name');
		$where['order_id'] = I('order_id');
		$where['open_id'] = I('open_id');
		$q = I('q');
		$j = I('j');
		if($q == 1){
			$status = 1;
		}
		if($j == 1){
			$status = -1;
		}
		$re = M('Order_tui')->where($where)->save(array('status'=>$status));
		if($re){
			$data['status'] = 1;
			$data['msg'] = "操作成功，请尽快处理";
			$this->ajaxReturn($data);
		}else{
		    $data['status'] = -1;
		    $data['msg'] = "操作失败，请稍后再试";
		    $this->ajaxReturn($data);
		}
	}
}